
package com.seekting.gongzhu.serversdk;

import java.util.ArrayList;
import java.util.List;

import com.seekting.gongzhu.sdk.gson.Card;
import com.seekting.gongzhu.sdk.gson.CardNumber;
import com.seekting.gongzhu.sdk.gson.CardType;

public class CardUtil {

    public static List<Card[]> getCardsList(int userCount, int parks,
            boolean widthKing) {
        int countPerPark = getOneCardsCount(widthKing);

        int sumCount = parks * countPerPark;

        if (sumCount % userCount != 0) {
            throw new IllegalArgumentException("分不均匀");
        }
        int oneUserCardSize = sumCount / userCount;
        Card[] sumCards = getNoSortCards(parks, widthKing);
        List<Card[]> list = new ArrayList<Card[]>();
        for (int i = 0; i < userCount; i++) {
            Card[] cards = new Card[oneUserCardSize];
            for (int j = 0; j < cards.length; j++) {
                cards[j] = sumCards[i * oneUserCardSize + j];
            }
            list.add(cards);
        }

        return list;
    }

    public static int getOneCardsCount(boolean widthKing) {
        CardType[] types = CardType.values();
        CardNumber[] cardNumber = CardNumber.values();
        return (widthKing ? 2 : 0) + (types.length - 2) * cardNumber.length;
    }

    public static Card[] getNoSortCards(int parks, boolean widthKing) {
        Card[] card = getCards(parks, widthKing);
        for (int j = 0; j < card.length; j++) {

            Card temp = card[j];
            int randomIndex = (int) (Math.random() * card.length);
            card[j] = card[randomIndex];
            card[randomIndex] = temp;

        }

        return card;

    }

    public static Card[] getCards(int parks, boolean widthKing) {

        CardType[] types = CardType.values();
        CardNumber[] cardNumber = CardNumber.values();

        int oneSize = (widthKing ? 2 : 0) + (types.length - 2) * cardNumber.length;
        Card[] cards = new Card[parks * oneSize];

        int cardIndex = 0;
        for (int j = 0; j < parks; j++) {

            int typeBegin = widthKing ? 0 : 2;
            int typeEnd = types.length;
            for (int type = typeBegin; type < typeEnd; type++) {

                CardType cardType = types[type];
                if (cardType == CardType.BigKing && widthKing) {
                    cards[cardIndex] = new Card(cardType, 0, j);
                    cardIndex++;
                } else if (cardType == CardType.SmallKing && widthKing) {
                    cards[cardIndex] = new Card(cardType, 0, j);
                    cardIndex++;
                } else {

                    for (int number = 1; number < cardNumber.length + 1; number++) {
                        cards[cardIndex] = new Card(cardType, number, j);
                        cardIndex++;
                    }
                }

            }
        }
        return cards;
    }

    public static void main(String[] args) {
        // System.out.println(getOneCardsCount(true));
        Card[] cards = getNoSortCards(1, false);
        // System.out.println(cards.length);
        for (Card card : cards) {
            // System.out.println(card);
        }

        List<Card[]> usercards = getCardsList(3, 1, false);
        for (Card[] cardss : usercards) {
            System.out.println("=========");
            for (Card card : cardss) {
                System.out.println(card);
            }
        }

    }
}
